<?php
// Module Body

function external_links_get_infotable(&$ret) {
	global $T;
	$current = Array('label' => $T['External Links'], 'fieldname' => 'external_links', 'url' => 'external');
	$ret['entries'][] = $current;
}

function external_links_domain_settings(&$data) {
	global $T;
	if (isset($data['domaindata']['external_links'])) {
		$val = $data['domaindata']['external_links'];
	} elseif (isset($data['data']['external_links'])) {
		$val = $data['data']['external_links'];
	}
	if ($data['userdata']['level'] < 3) {
		$value = '<input type="text" name="data[external_links]" size="5" maxlength="255" value="';
		if ($val != -1) {
			$value .= intval($val);
		}
		$value .= '" id="external" onchange="document.getElementById(\'externalcheck\').checked = false;">&nbsp;<input type=checkbox id="externalcheck" name="data[external_links]" value="-1" onclick="document.getElementById(\'external\').value = \'\'"';
		if ($val == -1) {
			$value .= ' checked"';
		}
		$value .= '>'.$T['Unlimited'];
	} else {
		$value = (($val == -1) ? $T['Unlimited'] : intval($val));
	}
	$data['entries'][] = Array('name' => $T['Number of External Links'], 'value' => $value);
}

function external_links_set(&$data) {
	set_property('external_links', $data['external_links'], $data['id'], '');
}

function external_links_fetch(&$data) {
	if ($data['type'] == 'reseller') {
		$where = substr($data['where'],0,5);
	} else {
		$where = $data['where'];
	}
	$external_links = get_property('external_links', $where, '');
	$data['name'][] = 'external_links';
	$data['value'][] = $external_links;
	return true;
}

if (!function_exists('external_links_fetch_alloc')) {
function external_links_fetch_alloc(&$data) {
	$where = $data['where'];
	if ($where == 0) {
		$SQL = 'SELECT sum(properties.value) as external_links FROM properties';
		if ($res = mysql_query($SQL)) {
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$external_links = $row['external_links'];
			}
		}
	} elseif (strlen($where) == 5) {
		$SQL = 'SELECT sum(properties.value) as external_links FROM properties inner join domains ON properties.id = domains.id WHERE domains.owner = '.$where.' AND properties.value != -1 AND properties.name = "external_links"';
		if ($res = mysql_query($SQL)) {
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$external_links = $row['external_links'];
			}
		}
	} else {
		$external_links = get_property('external_links', $where, '');
	}
	$data['name'] = 'external_links';
	$data['value'] = $external_links;
	return true;
}
}

if (!function_exists('external_links_fetch_total')) {
function external_links_fetch_total(&$data) {
	$where = $data['where'];
	if ($where == 0) {
		$where = '%';
	} elseif (strlen($where) == 5) {
		$where = substr($where,0,5).'%';
	}
	$SQL = 'SELECT count(id) as external_links FROM links WHERE owner LIKE "'.$where.'"';
	if ($res = mysql_query($SQL)) {
		if (mysql_num_rows($res)) {
			$row = mysql_fetch_array($res);
			$external_links = $row['external_links'];
		} else {
			$external_links = 0;
		}
	} else {
		$external_links = 0;
	}
	$data['name'] = 'external_links';
	$data['value'] = $external_links;
	return true;
}
}

if (!function_exists('external_links_check')) {
function external_links_check(&$data) {
	$external_links = get_property('external_links', $data['id'], '');
	if ($external_links == $data['external_links']) {
		return '';
	} else {
		return 'change';
	}
}
}

if (!function_exists('external_links_domain_check')) {
function external_links_domain_check(&$data) {
	global $T;
	$res =  Array();
	$external_links = get_property('external_links', $data['id'], '');
	if ($external_links != '' && $external_links != $data['external_links']) {
		$res['change'] = 'change';
	}
	$reseller_total = fetchdata('external_links','reseller', substr($data['id'],0,5));
	$reseller_total = $reseller_total['external_links'];
	$reseller_alloc = fetchdata('external_links','alloc', substr($data['id'],0,5));
	$reseller_alloc = $reseller_alloc['external_links'];
	$domain_alloc = fetchdata('external_links','domain', $data['id']);
	$domain_alloc = $domain_alloc['external_links'];
	$available_links = $reseller_total - $reseller_alloc + $domain_alloc;
	if (($reseller_total != -1) && (($data['external_links'] > $available_links) || $data['external_links'] == -1)) {
		$res['error'] = $T['err']['Not enough external links.'];
	}
	return $res;
}
}

if (!function_exists('external_links_reseller_settings')) {
function external_links_reseller_settings(&$data) {
	global $T;
	if (isset($data['resellerdata']['external_links'])) {
		$val = $data['resellerdata']['external_links'];
	} elseif (isset($data['data']['external_links'])) {
		$val = $data['data']['external_links'];
	}
	if ($data['userdata']['level'] < 2) {
		$value = '<input type="text" name="data[external_links]" size="5" maxlength="255" value="';
		if ($val != -1) {
			$value .= intval($val);
		}
		$value .= '" id="external" onchange="document.getElementById(\'externalcheck\').checked = false;">&nbsp;<input type=checkbox id="externalcheck" name="data[external_links]" value="-1" onclick="document.getElementById(\'external\').value = \'\'"';
		if ($val == -1) {
			$value .= ' checked"';
		}
		$value .= '>'.$T['Unlimited'];
	} else {
		$value = (($val == -1) ? $T['Unlimited'] : intval($val));
	}
	$data['entries'][] = Array('name' => $T['Number of External Links'], 'value' => $value);
}
}

function external_links_add_menu(&$data) {
	global $menu;

	if ($data['resellerdata']['external_links']) {
		$menu['reseller'][] = 'external';
	}
	if ($data['domaindata']['external_links']) {
		$menu['domain'][] = 'external';
	}
}

if (!function_exists('external_links_add_lang')) {
function external_links_add_lang(&$data) {
	global $T;
	switch($data['lang']) {
		default:
			/*if ($data['resellerdata']['external_links']) {
				$T['reseller']['external'] = 'External Links';
			}
			if ($data['domaindata']['external_links']) {
				$T['domain']['external'] = 'External Links';
			}*/
			$T['menu']['external'] = 'External Links';

			$T['Number of External Links'] = 'Number of External Links';
			$T['External Links'] = 'External Links';
			$T['err']['Not enough external links.'] = 'Not enough external links available.';
			break;
	}
}
}
?>